<template>
  <div class="dialog-box" style="position: relative;">
    <el-scrollbar class="custom-scroll content-box">
      <el-form
        ref="formEditPmDictTypeRef"
        :model="formData"
        :size="defaultFormItemSize"
        :rules="rules"
        label-width="120px"
        label-position="right"
        @submit.native.prevent
      >
        <el-row :gutter="16">
          <el-col :span="24">
            <el-form-item label="字典名称" prop="PmDictType.dictName">
              <el-input
                class="input-item"
                v-model="formData.PmDictType.dictName"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="字典分类" prop="PmDictType.dictCategory">
              <el-select
                class="input-item"
                v-model="formData.PmDictType.dictCategory"
                placeholder=""
                :clearable="true"
                :filterable="true"
                @visible-change="formEditPmDictType.dictCategoryWidget.onVisibleChange"
              >
                <el-option
                  v-for="item in formEditPmDictType.dictCategoryWidget.dropdownList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="PmDictType.remark">
              <el-input
                class="input-item"
                v-model="formData.PmDictType.remark"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength=""
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-scrollbar>
    <el-row class="footer-box" type="flex" justify="end" align="middle">
      <el-button :size="defaultFormItemSize" @click="onCancel()">取消</el-button>
      <el-button :size="defaultFormItemSize" type="primary" @click="onSubmitPmDictTypeClick()">保存</el-button>
    </el-row>
  </div>
</template>

<script>
import rules from '@/utils/validate';
import { stringCase, treeDataTranslate, findItemFromList, findTreeNodePath, findTreeNode } from '@/utils';
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins';
import { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js';
import { PmDictDataController, PmDictTypeController, DictionaryController } from '@/api';

export default {
  name: 'formEditPmDictType',
  props: {
    dictId: {
      default: undefined
    },
    subPage: {
      type: [Number, String, Boolean],
      default: 0
    },
    saveOnSubmit: {
      type: Boolean,
      default: true
    },
    rowData: {
      type: Object
    }
  },
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  data() {
    return {
      formData: {
        PmDictType: {
          // 主键ID
          dictId: undefined,
          // 字典名称
          dictName: undefined,
          // 字典分类(L-列表字典,T-树形字典)
          dictCategory: undefined,
          // 备注
          remark: undefined,
          isDatasourceInit: false
        }
      },
      formEditPmDictType: {
        // 字典分类(L-列表字典,T-树形字典)组件
        dictCategoryWidget: new DropdownWidget(this.loadDictCategoryDropdownList),
        isInit: false
      },
      rules: {
        'PmDictType.remark': [
        ],
        'PmDictType.dictCategory': [
          {required: true, message: '请输入字典分类(L-列表字典,T-树形字典)', trigger: 'blur'}
        ],
        'PmDictType.dictName': [
          {required: true, message: '请输入字典名称', trigger: 'blur'}
        ]
      },
    };
  },
  methods: {
    onCancel(isSuccess = false, data = null) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess, data);
      }
    },
    /**
     * 重置过滤值
     */
    resetFormEditPmDictType() {
      this.refreshFormEditPmDictType();
    },
    /**
     * 重置所有过滤值
     */
    resetFilter () {
      this.resetFormEditPmDictType();
    },
    /**
     * 保存
     */
    onSubmitPmDictTypeClick() {
      this.$refs.formEditPmDictTypeRef.validate((valid) => {
        if (!valid) return;
        // 级联操作
        if (!this.saveOnSubmit) {
          let formData = {
            ...this.formData.PmDictType
          };
        formData.dictCategoryDictMap = findItemFromList(this.formEditPmDictType.dictCategoryWidget.dropdownList, formData.dictCategory, 'id');
          this.onCancel(true, formData);
          return;
        }
        let params = {
          pmDictTypeDto: {
            dictId: this.formData.PmDictType.dictId,
            dictName: this.formData.PmDictType.dictName,
            dictCategory: this.formData.PmDictType.dictCategory,
            remark: this.formData.PmDictType.remark,
          }
        };

        let httpCall = this.isEdit ? PmDictTypeController.update : PmDictTypeController.add;
        httpCall(this, params).then(res => {
          this.$message.success('保存成功');
          this.onCancel(true);
        }).catch(e => {});
      });
    },
    loadPmDictTypeData() {
      return new Promise((resolve, reject) => {
        if (!this.isEdit) {
          resolve();
          return;
        }
        if (!this.saveOnSubmit && this.rowData != null) {
          this.formData.PmDictType = JSON.parse(JSON.stringify(this.rowData));
          resolve();
          return;
        }
        let params = {
          dictId: this.dictId
        };
        PmDictTypeController.view(this, params).then(res => {
          this.formData.PmDictType = {...res.data, isDatasourceInit: true};
          resolve();
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 字典分类(L-列表字典,T-树形字典)下拉数据获取函数
     */
    loadDictCategoryDropdownList() {
      return Promise.resolve(this.PmDictType.getList());
    },
    onUploadError (e, file, fileList) {
      this.$message.error('文件上传失败');
    },
    onUploadLimit (files, fileList) {
      this.$message.error('已经超出最大上传个数限制');
    },
    refreshFormEditPmDictType(reloadData = false) {
    },
    formInit() {
      this.loadPmDictTypeData().then(res => {
        this.formEditPmDictType.dictCategoryWidget.onVisibleChange(true).catch(e => {});
        if (this.isEdit) this.refreshFormEditPmDictType();
      }).catch(e => {});
    }
  },
  computed: {
    isEdit() {
      return this.saveOnSubmit ? this.dictId != null : this.rowData != null;
    }
  },
  mounted() {
    // 初始化页面数据
    this.formInit();
  }
};
</script>
